# coding : UTF-8
"""
author：BingBO   time：2022.11.11
Theme：
notes：
"""
import cv2 as cv
import numpy as np


def laser1_Extract(L1img):
    print("********************** laser1_ 特征提取 **********************")
    Red = L1img[:, :, 2]  # B0 G1 R2
    _, threshold = cv.threshold(Red, 190, 255, cv.THRESH_BINARY)  # 转为二值图
    laser1_threshold = cv.medianBlur(threshold, 3)

    cv.imshow('laser1_threshold', laser1_threshold)
    cv.imwrite(r"F:\MVS_Data\laser1_threshold.jpg", laser1_threshold)

    hight, width = laser1_threshold.shape
    # 将上面的重叠光条去掉
    for col in range(width):
        n = 0
        for row in range(hight - 1, 0, -1):
            diff = int(laser1_threshold[row - 1, col]) - int(laser1_threshold[row, col])  # 上一行像素值减去下一行像素值
            if diff > 0:
                n += 1
            if n > 1 and laser1_threshold[row, col] != 0:  # 将上面的重叠光条去掉
                laser1_threshold[row, col] = 0
    cv.imshow('remove', laser1_threshold)
    # cv.waitKey(0)
    cv.destroyAllWindows()

    '''
    # 将下面的重叠光条去掉
    for col in range(width):
        n = 0
        for row in range(hight - 1):
            diff = int(laser1_threshold[row + 1, col]) - int(laser1_threshold[row, col])  # 下一行像素值减去上一行像素值
            if diff > 0:
                n += 1
            if n > 1 and laser1_threshold[row, col] != 0:  # 将上面的重叠光条去掉
                laser1_threshold[row, col] = 0
    cv.imshow('remove', laser1_threshold)
    cv.waitKey(0)
    '''

    # 求laser1光条的下间断点行坐标
    lightList = np.sum(laser1_threshold, axis=1)  # 1 行相加, 变成一列
    ret = np.where(lightList > 0)  # 返回（索引+ 数据类型 ）
    lightIdx = ret[0]
    # print("lightIdx:", lightIdx)
    RightShift = np.roll(lightIdx, 1)  # 向右移动一位
    # print("RightShift:", RightShift)
    differList = lightIdx - RightShift
    # print("differList:", differList)
    temp = differList.tolist()
    row_gapDn = lightIdx[temp.index(max(differList))]

    # 计算这一行的特征点坐标
    n, sumRow = 0, 0
    for col in range(width):  # 遍历这一行的每一列
        if laser1_threshold[row_gapDn, col] != 0:
            n += 1
            sumRow += (col + 1)  # 重心在第几列
    indexCol = int(sumRow / n) - 1  # 化为坐标索引

    P1 = [indexCol, row_gapDn]
    print("laser1_的焊缝特征点P1：", P1)
    cv.circle(L1img, P1, 1, (255, 0, 255), -1)
    cv.circle(L1img, P1, 5, (255, 0, 255), 1)

    # cv.imshow('P1_L1img', L1img)
    cv.imwrite(r"F:\MVS_Data\P1_L1img.png", L1img)

    return P1


def laser2_Extract(L2img):
    print("********************** laser2_ 特征提取 **********************")
    Red = L2img[:, :, 2]  # B0 G1 R2
    _, threshold = cv.threshold(Red, 190, 255, cv.THRESH_BINARY)  # 转为二值图
    laser2_threshold = cv.medianBlur(threshold, 3)

    # cv.imshow('laser2_threshold', laser2_threshold)
    cv.imwrite(r"F:\MVS_Data\laser2_threshold.jpg", laser2_threshold)

    # 求laser2光条的左间断点列坐标
    lightList = np.sum(laser2_threshold, axis=0)  # 0 列相加, 变成一行
    ret = np.where(lightList > 0)  # 返回（索引+ 数据类型 ）
    lightIdx = ret[0]
    # print("lightIdx:", lightIdx)
    LeftShift = np.roll(lightIdx, -1)  # 向左移动一位
    # print("LeftShift:", LeftShift)
    differList = lightIdx - LeftShift
    # print("differList:", differList)
    temp = differList.tolist()
    col_gapLeft = lightIdx[temp.index(min(differList))]

    # 再将间断点右边的 光条排除
    laser2_ = laser2_threshold[:, : col_gapLeft + 1]  # 刚好截止到 左间断点

    lightList_part = np.sum(laser2_, axis=1)  # 1 行相加, 变成一列
    ret = np.where(lightList_part > 0)  # 返回（索引+ 数据类型 ）
    lightIdx = ret[0]
    rowIdx = lightIdx[0]

    # 计算这一行的特征点坐标
    _, width = laser2_.shape
    n, sumCol = 0, 0
    for col in range(width):  # 遍历这一行的每一列
        if laser2_[rowIdx, col] != 0:
            n += 1
            sumCol += (col + 1)  # 重心在第几列
    indexCol = int(sumCol / n) - 1  # 化为坐标索引

    P2 = [indexCol, rowIdx]
    print("laser2_的焊缝特征点P2：", P2)
    cv.circle(L2img, P2, 1, (255, 0, 255), -1)
    cv.circle(L2img, P2, 5, (255, 0, 255), 1)

    # cv.imshow('P2_L2img', L2img)
    cv.imwrite(r"F:\MVS_Data\P2_L2img.png", L2img)

    return P2
